import QtQuick
import "./Promise.js" as P
import "./Utils.js" as U
import "./Rest.js" as Rest

QtObject {
    property string baseUrl: ApiService.getBaseUrl()
    property var newMusicCache: []
    property var selectedPlaylistsCache: []
    property var playlistInfoCache: []
    //获得最新音乐
    function getNewMusic(type) {
        type = type || "0";
        const cache = newMusicCache.filter(t => type === t.type);
        if (cache.length !== 0) {
            return new P.Promise((res, rej) => res(cache[0].data));
        }
        return Rest.get(baseUrl + "/top/song", {
            "type": type
        }).then(res => {
            const data = res.data.map(obj => {
                return {
                    "id": obj.id,
                    "name": obj.name,
                    "artists": obj.artists.map(ar => ar.name).join("/"),
                    "album": obj.album.name,
                    "coverImage": obj.album.picUrl,
                    "allTime": U.timeFormat(obj.hMusic?.playTime),
                    "vip": obj.fee === 1 ? 1 : 0
                };
            });
            newMusicCache.push({
                type: type,
                data: data
            });
            return data;
        });
    }
    //获取精选歌单
    function getSelectedPlaylists(cat) {
        cat = cat || "全部";
        const cache = selectedPlaylistsCache.filter(t => cat === t.cat);
        if (cache.length !== 0) {
            return new P.Promise((res, rej) => res(cache[0].data));
        }
        return Rest.get(baseUrl + "/top/playlist/highquality", {
            "cat": cat
        }).then(res => {
            const data = res.playlists.map(obj => {
                return {
                    "id": obj.id,
                    "name": obj.name,
                    "coverImage": obj.coverImgUrl,
                    "description": obj.description
                };
            });
            selectedPlaylistsCache.push({
                cat: cat,
                data: data
            });
            return data;
        });
    }

    //获取歌单详情
    function getPlayerListDetail(id) {
        const cache = playlistInfoCache.filter(t => id === t.id);
        if (cache.length !== 0) {
            return new P.Promise((res, rej) => res(cache[0].data));
        }
        return Rest.get(baseUrl + "/playlist/detail", {
            "id": id
        }).then(res => {
            // print(JSON.stringify(res))
            const p = res.playlist;
            const data = {
                id: p.id,
                name: p.name,
                //歌单名称
                coverImgUrl: p.coverImgUrl,
                //封面地址
                trackCount: p.trackCount,
                //歌单数量
                description: p.description,
                //介绍
                lastUpdateDate: U.dateFormat(p.trackNumberUpdateTime),
                //上次更新歌单时间
                tags: p.tags.join("/"),
                //标签
                playCount: p.playCount,
                //播放数量
                shareCount: p.shareCount,
                //分享数量
                commentCount: p.commentCount,
                //评论数量
                tracks: p.tracks.map(obj => ({
                            "id": obj.id,
                            "name": obj.name,
                            "artists": obj.ar.map(ar => ar.name).join("/"),
                            "album": obj.al.name,
                            "coverImage": obj.al.picUrl,
                            "allTime": U.timeFormat(obj.dt),
                            "vip": obj.fee === 1 ? 1 : 0        // vip音乐
                        }))                           //歌曲列表
            };
            playlistInfoCache.push({
                id: id,
                data: data
            });
            return data;
        });
    }

    //根据音乐id获取播放地址
    function getMusicUrl(id) {
        return Rest.get(baseUrl + "/song/url", {
            "id": id
        }).then(res => {
            return res.data[0].url;
        });
    }

    //返回热门搜索关键字
    function getSearchHot() {
        return Rest.get(baseUrl + "/search/hot/detail").then(res => res.data.map(v => ({
                        text: v.searchWord
                    })));
    }
    //获取搜索建议
    function getSearchSuggest(keywords) {
        return Rest.get(baseUrl + "/search/suggest", {
            keywords: keywords
        }).then(res => {
            if (res.result.songs === undefined)
                return [];
            return res.result.songs.map(v => ({
                        name: v.name,
                        author: v.artists.map(a => a.name).join("/")
                    }));
        });
    }
    //获取搜索结果
    function getSearchList(keywords, limit = 30, offset = 0) {
        return Rest.get(baseUrl + "/cloudsearch", {
            keywords: keywords,
            type: 1,
            limit: limit,
            offset: offset
        }).then(res => res.result.songs.map(obj => ({
                        "id": obj.id,
                        "name": obj.name,
                        "artists": obj.ar.map(ar => ar.name).join("/"),
                        "album": obj.al.name,
                        "coverImage": obj.al.picUrl,
                        "allTime": U.timeFormat(obj.dt),
                        "vip": obj.fee === 1 ? 1 : 0       // vip音乐
                    })));
    }

    Component.onCompleted: {
        console.log("baseUrl:" + baseUrl);
    }
}
